package spring5.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import spring5.dao.UserDao;

@Service
@Transactional(readOnly = false, timeout = -1, propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ) //事务注解
public class UserService {

    //注入Dao
    @Autowired
    private UserDao userDao;

    //转账的方法
    public void accountMoney() {
        /*编程式事务管理*/
        /*try {
            //一、开启事务
            //二、进行业务操作
            //lucy少100
            userDao.reduceMoney();
            //异常
            int i = 10 / 0;
            //mary多100
            userDao.addMoney();
            //三、没有异常，事务提交
        } catch (Exception e) {
            //四、出现异常，事务回滚
        }*/

        /*声明式事务管理*/
        //lucy少100
        userDao.reduceMoney();
        //异常
        int i = 10 / 0;
        //mary多100
        userDao.addMoney();
    }
}
